from utils import Spider, get_singer_song_list
from utils.song_url_spider import SongUrlSpider
import config
import os
import json
from models.tables import SingerModel, MusicModel
from models import db
import sys
import time
from sqlalchemy import func


def run():
    # spider = Spider()
    start = 1
    end = 50000
    if len(sys.argv) > 1:
        start = int(sys.argv[1])
        end = int(sys.argv[2])
    start_time = int(time.time())
    url_spider = SongUrlSpider(start, end)
    url_spider.run()
    end_time = int(time.time())
    diff = end_time - start_time
    print('diff', diff)
    # start_time = int(time.time())
    # spider.get_all_song_url(start, end)
    # end_time = int(time.time())
    # diff = end_time - start_time
    # print('diff', diff)

    # print('start', str(int(time.time())))
    # count = db.query(func.count('*')).select_from(MusicModel).scalar()
    # print(type(count))
    # all_singer = db.query(MusicModel).filter_by(id=1).one()
    # print(all_singer.song_name)
    # print('count', count)
    # print('end', str(int(time.time())))
    # start = 0
    # if len(sys.argv) > 1:
    #     start = sys.argv[1]
    # spider.get_all_song(int(start))
    # singer = db.query(SingerModel).filter_by(singer_mid="003U6coz1AhN3H").one()
    # print('singer', singer.singer_name)
    # spider.get_singer_song_list(singer.singer_mid, singer.singer_name, 0)
    # spider.get_all_singer()
    pass
    # music = db.query(MusicModel).all()
    # print('len', len(music))


def add_music():
    add_list = []
    single_song = db.query(MusicModel).filter_by(id=1).one()
    for _ in range(1, 10000):
        song = MusicModel(song_mid=single_song.song_mid,
                          song_name=single_song.song_name,
                          song_info=single_song.song_info,
                          album_mid=single_song.album_mid,
                          album_name=single_song.album_name,
                          singer_mid=single_song.singer_mid,
                          singer_name=single_song.singer_name,
                          song_url='',
                          my_song_url='',
                          state=single_song.state
                          )
        add_list.append(song)
    db.add_all(add_list)
    db.commit()

if __name__ == '__main__':
    run()
